package com.asm.netty.g_exercise;

import io.netty.bootstrap.ServerBootstrap;
import io.netty.buffer.ByteBuf;
import io.netty.channel.*;
import io.netty.channel.nio.NioEventLoopGroup;
import io.netty.channel.socket.nio.NioServerSocketChannel;
import io.netty.channel.socket.nio.NioSocketChannel;
import lombok.extern.slf4j.Slf4j;
import org.junit.Test;

import java.nio.charset.Charset;
import java.util.concurrent.TimeUnit;

@Slf4j
public class Server {

    public static void main(String[] args) {

        new ServerBootstrap()
                .group(new NioEventLoopGroup())
                .channel(NioServerSocketChannel.class)
                .childHandler(new ChannelInitializer<NioSocketChannel>() {
                    @Override
                    protected void initChannel(NioSocketChannel ch) throws Exception {
                        // 以下两个handler来回踢皮球了
//                        ch.pipeline().addLast(new ChannelOutboundHandlerAdapter(){
//                            @Override
//                            public void write(ChannelHandlerContext ctx, Object msg, ChannelPromise promise) throws Exception {
//                                System.out.println("head ChannelOutboundHandlerAdapter");
//                                ctx.fireChannelRead(msg);
//                            }
//                        });
                        ch.pipeline().addLast(new ChannelInboundHandlerAdapter(){
                            @Override
                            public void channelRead(ChannelHandlerContext ctx, Object msg) throws Exception {
//                                super.channelRead(ctx, msg);
                                ByteBuf buffer = (ByteBuf) msg;
                                log.debug(buffer.toString(Charset.defaultCharset()));

                                // 建议使用 ctx.alloc() 创建 ByteBuf
                                final ByteBuf response = ctx.alloc().buffer(); // class io.netty.buffer.PooledUnsafeDirectByteBuf
                                response.writeBytes("服务端:".getBytes());
                                response.writeBytes(buffer);
                                TimeUnit.SECONDS.sleep(20);
                                ctx.writeAndFlush(response);
                            }
                        });
                    }
                })
                .bind(8080);
    }

    @Test
    public void onlyTest()
    {
        String a = "abcdefg";
        System.out.println(a);

        System.exit(0);
        if(true)return;

        String b = "hijklmn";
        System.out.println(b);
    }
}
